<?php
class Tools extends MY_Controller{
	
	function __construct()
	{
		parent::__construct();
		if(!$this->tool) redirect('tool/login');
	}
	
  function by_month($year=null,$month=null){

    	if($year==null || $month==null){
        			$month=date('m');
        		 $year = date('Y');
        		}
        		
    	$data['title'] = 'Commandes';
	
		$data['calendar'] = order::generate_tool($year,$month);
    	$this->load->view('tool/header',$data);
		$this->load->view('tool/orders/index',$data);
	
    }
    
function date($year=null,$month=null,$day =null)
	{
		if($year != null && $month != NUll && $day != NULL){
		$data['title'] = 'Commandes';
		$data['admin'] = $this->admin;
		$today = new ActiveRecord\DateTime(($year.'-'.$month.'-'.$day));
	 	$start_today = $today->format('Y-m-d')." 00:00:00";
		$end_today = $today->format('Y-m-d')." 23:59:59"; 
		$data['orders'] = order::find('all', array('conditions'=>array('order_time >=? AND order_time <?',$start_today, $end_today),'limit' => '', 'offset' => '','order'=>'shop_id desc'));
		$total = order::find('all', array('select'=>'total, discount','conditions'=>array('order_time >=? AND order_time <?',$start_today, $end_today),'order'=>'order_num desc'));
		$total_sum = array();
		for($i=0;$i<count($total);$i++){
			$total_sum[$i] = $total[$i]->total*(1-$total[$i]->discount);
		}
		
		$data['date'] = $today->format('d - M - Y');
		$data['date_format'] = $today->format('Y/m/d');
		$data['total_sum'] = $total_sum;
		$this->config->load('pagination');
		$config['per_page'] = 100; 
		$config['uri_segment'] = 7;
		$config['num_links'] = 4;
	 	$config['base_url'] = site_url('tool/tools/date/'.$year.'/'.$month.'/'.$day);
		$config['total_rows'] = count($data['orders']);
		$config['enable_query_strings']=true;
		$this->pagination->initialize($config); 
		$data['num'] = count($data['orders']);
	 	$data['page_links']= $this->pagination->create_links();
	 	$data['orders'] = order::find('all', array('conditions'=>array('order_time >=? AND order_time <?',$start_today, $end_today),'limit' => $config['per_page'], 'offset' =>$this->uri->segment(7),'order'=>'order_num asc'));
		$this->load->view('tool/header',$data);
		$this->load->view('tool/orders/orders',$data);
		 
		
		}else{
		redirect('admin990/orders');
		}
		
	}

	function order_details()
	{
		$data['title'] = 'Commandes';
		$order_num = $this->uri->segment(7);
		$order_info = order::find_by_order_num($order_num);
		$data['order_info'] = $order_info;
		if($order_info->address_id !=NULL){
		$data['address'] = adresse::find_by_id($order_info->address_id);	
		}
		$data['order'] = orderitem::get_details($order_num);
		$this->load->view('tool/header',$data);
		$this->load->view('tool/orders/order_details',$data);
		
	}
	
	function multi_exec()
	{
		if($_POST)
		{	
			$item = $_POST['item'];
			
			foreach($item as $i)
			{
				orderitem::table()->delete(array('order_num' => $i));			
			}
				order::table()->delete(array('order_num' => $item));
			
			redirect($_POST['url']);
		}
	}
	
	
}